﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using WeMakeSP.ImageResizer.Configuration;
using WeMakeSP.ImageResizer.Events;
using WeMakeSP.Platform.SharePoint.Administration;
using WeMakeSP.Platform.SharePoint.Configuration;

namespace WeMakeSP.ImageResizer.Features
{
    /// <summary>
    /// The event receiver for the image resizer feature.
    /// </summary>
    public class ImageResizerFeatureEventReceiver : SPFeatureReceiver
    {
        #region Constants

        /// <summary>
        /// The item receiver class name.
        /// </summary>
        private const string ItemReceiverClassName = "WeMakeSP.ImageResizer.Events.ImageResizerItemEventReceiver";

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageResizerFeatureEventReceiver"/> class.
        /// </summary>
        public ImageResizerFeatureEventReceiver()
        {
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site =
             properties.Feature.Parent as SPSite;

            SPWebApplication webApplication =
                site.WebApplication;

            string featureName =
                properties.Feature.Definition.DisplayName;

            // Impersonate as the AppPool account of the web application.

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                // Breadcrumb.

                try
                {
                    UpdateLayoutSiteMap uls =
                        new UpdateLayoutSiteMap(webApplication);

                    uls.AddEntry(
                        WeMakeSP.ImageResizer._12.TEMPLATE.Features.ImageResizer.Resources.image_resizer.ir_image_resizer_settings,
                        "/_layouts/ImageResizer/img_resizer_settings.aspx",
                        "/_layouts/listedit.aspx");
                    uls.SubmitJob();
                }
                catch (Exception)
                {
                    // Ignore.
                }
            });

            // Copy the resources to the app bin.

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                CopyAppBinContent copyAppBinContent =
                    new CopyAppBinContent();

                copyAppBinContent.SubmitJob();
            });
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = 
                SPContext.Current.Site.WebApplication;

            SPWeb web = SPContext.Current.Web;

            // Delete existing jobs.

            SPJobDefinitionHelper.DeleteJobStartingWithName(
                webApp, 
                PictureLibraryResizerJob.Prefix);

            // Remove existing handlers.

            ImageResizerHandlers.RemoveHandlersFromWeb(web);

            // Delete property bags.

            ImageResizerPropertyBags.DeleteConfigurationsFromWeb(web);
        }

        #endregion
    }
}
